/**
 * Created by sunkejun on 2015/10/15.
 */

$(document).ready(function(){

    // activate Nestable for list 1
    $('#nestable').nestable({
        group: 1
    }).on('change',function(e){
       console.log($(e.target));
    })
    admin_category_list.getCategoryTree();

})
var admin_category_list  = (function ($) {

    this.getCategoryTree = function() {
        $.ajax({
            url:  '/admin/category/create',
            type:"GET",
            dataType:'JSON',
            data: {},
            success:function(response){
                initTree(response);
                $('#nestable').html(initSortList(response) );

            }
        });
    }
    this.saveSortedCategory = function(){
        ComdataUtil.ajaxpost('/admin/category/updateCtrees',{data:JSON.stringify($('#nestable').nestable('serialize'))},this.finishAjaxLoadBack);
    }

    this.update = function(id){
        ComdataUtil.clear("categoryform");
        ComdataUtil.ajaxpostnormal('/admin/category/'+id,{},function(data){
            ComdataUtil.loadContainer("categoryform",data);
        },'GET');
    }
    this.initSortList = function(data){
        var str = '<ol class="dd-list">';
        //style="position: absolute;right: 2px;z-index: 1024"
        for(var i=0;i<data.length;i++){

            str+=' <li class="dd-item dd-collapsed"  data-id="'+data[i].id+'">';
            if(data[i].children &&  data[i].children.length >0){
                str+= '<button data-action="collapse" type="button" style="display: none;">Collapse</button><button data-action="expand" type="button" style="display: block;">Expand</button>';
            }
            str+='<div style="" class="dd-handle" > '+data[i].name+'</div>';

            if(data[i].children &&  data[i].children.length >0){
                str+= initSortList(data[i].children);
            }

            str+=' <a class="label label-primery" style="position: absolute;right: 0;top: 5px;" onclick="admin_category_list.update('+data[i].id+')"><i class="fa fa-edit"></i></a>';
            //str+='<a style="position: absolute;right: 0;top: 5px;">xiug </a>';
             str+=' </li>';
        }
        str+=" </ol>";
        return str;
    }
    this.initTree = function(data){
        var setting = {
            callback: {
                onClick: this.treeCategorySelect
            },
            data: {
                simpleData: {
                    enable: true,
                    idKey: "id",
                    pIdKey: "parent_id"
                }
            }
        };
        var zNodes =data;
        this.$tree = $.fn.zTree.init($("#category_tree"), setting, zNodes);
        var nodes = this.$tree.getNodesByParam("id", $("#parent_id").val(), null);

        if(nodes.length >0){
            this.$tree.selectNode(nodes[0], true, true);
        }
    }
    this.treeCategorySelect = function(event, treeId, treeNode){
        //alert(treeNode.tId + ", " + treeNode.name + "," + treeNode.checked);

        $("#category_modal_current").html(treeNode.name);

    }
    this.selectNode = function(){
        var nodes = this.$tree.getSelectedNodes();
        if(nodes.length >0){
            $("#parent").val(nodes[0].name);
            $("#parentid").val(nodes[0].id);
        }
        $("#category_modal").modal('hide');
    }
    this.openModel = function() {
         $("#category_modal").modal('show');
        console.log($("#parent_id").val());
        var nodes = this.$tree.getNodesByParam("id", $("#parent_id").val(), null);
        console.log(nodes);
        if(nodes.length >0){
            this.$tree.selectNode(nodes[0]);
        }
    }


    this.submitCategory = function(){
        $('#com_message').html("");
        var data = ComdataUtil.collectData("categoryform");
        ComdataUtil.ajaxpost('/admin/category',data,this.finishAjaxLoadBack);
    }
    this.finishAjaxLoadBack = function(response){
        if(response.code == 500){
            $('#com_message').attr("class","text-danger text-center");
            $('#com_message').html(response.res_data);
        }else{
            ComdataUtil.clear("categoryform");
            getCategoryTree();
        }

    }
    this.resetCategory = function(){
       ComdataUtil.clear("categoryform");
    }

    return this;
})(jQuery);

